#include<stdio.h>
#include<math.h>
double qiu_d2(int y0,int xr,int xa)
{
	return (((double)(y0*xr)-(double)(xa*y0))*((double)(y0*xr)-(double)(xa*y0)))/((double)(xa*xa)+(double)(y0*y0));
}
double qiu_a(double d2,int xr,int xa,int r)
{
	return sqrt((xr-xa)*(xr-xa)-d2)-sqrt(r*r-d2);
}
double qiu_b(double d2,int xr,int xa,int r)
{
	return 2*sqrt(r*r-d2);
}
int main(int argc,char *argv[])
{
    int t;
	double d2;
	double sum=0;
	double temp;
	int xa,ya,xr,r,n,y0;
	int i;
	double a;
	double b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d %d %d",&xa,&ya,&xr,&r,&n);
        int c;
        for(i=1; i<=n; i++)
        {
            temp=0;
            scanf("%d %d",&y0,&c);
            y0=y0-ya;
            d2=qiu_d2(y0,xr,xa);
            if(d2<r*r)
            {
                a=qiu_a(d2,xr,xa,r);
                b=qiu_b(d2,xr,xa,r);
                if(a*a+a*b<=0)
                {
                    temp=0;
                }
                else
                {
                    temp=a*a+a*b;
                }
            }
            sum+=c-temp;
        }
        printf("%.0f\n",sum);
    }
	return 0;
}

